/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package threads;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import main.User;

/**
 *
 * @author x3m
 */
public class UserCleaner implements Runnable {

    private ArrayList<User> _users = null;

    public UserCleaner(ArrayList<User> users) {
        this._users = users;
    }

    private static Date add(Date date, int calendarField, int amount) {
        if (date == null) {
            throw new IllegalArgumentException("The date must not be null");
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(calendarField, amount);
        return c.getTime();
    }

    @Override
    public void run() {
        while (true) {
            Date now = new Date();
            for (int i = 0; i < _users.size(); i++) {
                
                Date last = add(_users.get(i).getLastUpdate(), Calendar.MINUTE, 1);
                if (last.before(now)) {
                    _users.remove(i);
                }
                
            }
            try {
                Thread.sleep(3000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }
}
